package com.zhongde.cloud89app;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.zhongde.cloud89app.R;
import com.zhongde.cloud89app.adapter.MyViewPagerAdapter;
import com.zhongde.cloud89app.fragment.HomeFragment;
import com.zhongde.cloud89app.fragment.PersionFragment;
import com.zhongde.cloud89app.fragment.SetFragment;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private BottomNavigationView my_bottomNavigationView;
    private ViewPager viewPager;

    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_bottomNavigationView = findViewById(R.id.my_bottomNavigationView);
        viewPager = findViewById(R.id.fragment_view);
        viewPager.setOffscreenPageLimit(2);
        fragments.add(new HomeFragment());
        //   fragments.add(new SetFragment());
        fragments.add(new PersionFragment());
        viewPager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(this);

        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        setBottomNavigationView();
    }

    private void setBottomNavigationView() {
        my_bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        viewPager.setCurrentItem(0);
                        break;
//                    case R.id.navigation_dashboard:
//                        viewPager.setCurrentItem(1);
//                        break;
                    case R.id.navigation_notifications:
                        viewPager.setCurrentItem(2);
                        break;

                    default:
                        break;

                }
                return true;

            }
        });
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                my_bottomNavigationView.setSelectedItemId(R.id.navigation_home);
                break;
//            case 1:
//                my_bottomNavigationView.setSelectedItemId(R.id.navigation_dashboard);
//                break;
            case 2:
                my_bottomNavigationView.setSelectedItemId(R.id.navigation_notifications);
                break;

        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    private long exitTime = 0;


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再次返回退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}